package com.ruix.app.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.ruix.app.R;

/**
 * Created by hadexs on 2017/4/30.
 */

public class SavePhotoDialog extends Dialog implements View.OnClickListener{


    private int index;
    private SavePhotoListener savePhotoListener;
    private String qrcodeValue;
    private View saveQrCodeBtn;

    public SavePhotoDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_copy_comment);
        setCanceledOnTouchOutside(true);
        //初始化界面控件
        //初始化界面控件的事件
        initEvent();

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;//宽高可设置具体大小
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp);

    }

    private void initEvent(){
        TextView textView = (TextView)findViewById(R.id.tv_copy_comment);
        textView.setText("保存");
        textView.setOnClickListener(this);
        saveQrCodeBtn =  findViewById(R.id.ll_qrcode_contain);
        saveQrCodeBtn.setOnClickListener(this);
        findViewById(R.id.tv_cancel_action).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_copy_comment:
                dismiss();
                if (savePhotoListener != null){
                    savePhotoListener.savePhoto(index);
                }

                break;
            case R.id.ll_qrcode_contain:
                dismiss();
                if (savePhotoListener != null){
                    savePhotoListener.decodeQrcode(qrcodeValue);
                }

                break;

            case R.id.tv_cancel_action:
                dismiss();
                break;
        }
    }


    public void show(int comment){
        index = comment;
        show();
    }



    public void setSavePhotoListener(SavePhotoListener savePhotoListener) {
        this.savePhotoListener = savePhotoListener;
    }

    public interface  SavePhotoListener{
        void savePhoto(int position);
        void decodeQrcode(String qrcode);
    }


    public void setQrcodeValue(String qrcodeValue) {
        this.qrcodeValue = qrcodeValue;
        if (isShowing()){
            saveQrCodeBtn.setVisibility(View.VISIBLE);
        }
    }
}
